/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/7/22 19:53
 */
// 两两交换链表节点
    // 要求不改变节点值
    // https://leetcode.cn/problems/swap-nodes-in-pairs/
public class Solution24 {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = dummy;
        ListNode tmp;
        ListNode first;
        ListNode second;
        while (cur.next != null && cur.next.next != null) {
            // 记录交换节点对的下一个节点
            tmp = cur.next.next.next;
            first = cur.next;
            second = cur.next.next;
            // 交换节点
            cur.next = second;
            second.next = first;
            first.next = tmp;
            cur = first;
        }
        return dummy.next;
    }
}
